//
//  BiTabBarController.m
//  GottaWash
//
//  Created by bindu on 14-8-7.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiTabBarController.h"
#import "UIViewController+BiTabBarController.h"

@interface BiTabBarController ()
{
}

@end



@implementation BiTabBarController
{
    // Bottom tab bar view
    BiTabBar * _biTabBar;
}

- (id)init
{
    self = [super init];
    if (!self) return nil;
    
    return self;
}

- (void)loadView
{
    [super loadView];
    
    CGRect tabBarFrame = self.tabBar.frame;
    tabBarFrame.origin.y = 0;
    _biTabBar = [[BiTabBar alloc] initWithFrame:tabBarFrame];
    _biTabBar.delegate = self;
    [self.tabBar addSubview:_biTabBar];
}

- (void)resetTabs
{
    NSMutableArray *tabs = [[NSMutableArray alloc] init];
    for (UIViewController *vc in self.viewControllers)
    {
        BiTab *tab = [[BiTab alloc] init];
        [tab setTabImage:[vc tabImage]];
        [tab setTabSelectImage:[vc tabSelectImage]];
        [tab setTabTitle:[vc tabTitle]];
        
        [tab setTabBarHeight:K_TAB_BAR_HEIGHT];
        
        if ([[vc class] isSubclassOfClass:[UINavigationController class]])
            ((UINavigationController *)vc).delegate = self;
        
        [tabs addObject:tab];
        [tab release];
    }
    
    [_biTabBar setTabs:tabs];
    BiTab *tab = [_biTabBar.tabs objectAtIndex:0];
    tab.selected = YES;
    
    [self.tabBar bringSubviewToFront:_biTabBar];
}

- (void)refreshTabs
{
    for (int i = 0; i < _biTabBar.tabs.count; i++)
    {
        BiTab *tab = [_biTabBar.tabs objectAtIndex:i];
        
        if (self.selectedIndex == i)
        {
            tab.selected = YES;
        }
        else
        {
            tab.selected = NO;
        }
    }
}

- (void)setTabShowTipDot:(int)tabIndex show:(BOOL)show
{
    BiTab *tab = [_biTabBar.tabs objectAtIndex:tabIndex];
    tab.showTipDot = show;
    [tab setNeedsDisplay];
}

#pragma mark - Required Protocol Method

- (void)tabBar:(BiTabBar *)BiTabBarDelegate didSelectTabAtIndex:(NSInteger)index
{
    self.selectedIndex = index;
    
    if (index == 1)
    {
        [APP_DELEGATE setTabShowTipDot:1 show:NO];
    }
    
    [self refreshTabs];
}

@end
